package DBLag;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import ModelLag.*;


public class DbBehPlan {
	
	private Connection con;
	private DbIndlaeggelse Dbindlaeggelse;
	
	public DbBehPlan() {
		con = DbConnection.getInstance().getDBcon();
		Dbindlaeggelse = new DbIndlaeggelse();
	}
	
	public int tilfoejBehPlan(Handling behPlan) {
		int rc = -1;
		PreparedStatement pstmt = null;
		String insert = "insert into Handling (hId, iId, GennemfoertDato, PlanlagtDato, GennemfoertTidspunkt, PlanlagtTidspunkt, udloebsTidspunkt, revurdering, udloebsDato, handlingType)"
				+ "values (?,?,?,?,?,?,?,?,?,?)";
		System.out.println(insert);
		try {
			pstmt = con.prepareStatement(insert);
			pstmt.setInt(1, behPlan.gethId());
			pstmt.setInt(2, behPlan.getiId().getiId());
			pstmt.setString(3, behPlan.getGennemfoertDato());
			pstmt.setString(4, behPlan.getPlanlagtDato());
			pstmt.setString(5, behPlan.getGennemfoertTidspunkt());
			pstmt.setString(6, behPlan.getPlanlagtTidspunkt());
			pstmt.setString(7, behPlan.getUdloebsTidspunkt());
			pstmt.setString(8, behPlan.getRevurdering());
			pstmt.setString(9, behPlan.getUdloebsDato());
			pstmt.setString(10, "behandlings plan");

			rc = pstmt.executeUpdate();
		} catch (SQLException sqlE) {
			System.out.println(sqlE.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return rc;
	}

	public int opdaterbehPlan(Handling behPlan) {
		Handling hObj = behPlan;
		int rc = -1;
		String query = "UPDATE Handling SET " + "GennemfoertDato ='" + hObj.getGennemfoertDato()
				+ "', " + "PlanlagtDato ='"
				+ hObj.getPlanlagtDato() + "', " + "UdloebsDato ='"
				+ hObj.getUdloebsDato() + "' "
				+ ", " + "GennemfoertTidspunkt ='" + hObj.getGennemfoertTidspunkt()+ "', "
				+"PlanlagtTidspunkt ='" + hObj.getPlanlagtTidspunkt() + "', " 
				+"UdloebsTidspunkt ='" + hObj.getUdloebsTidspunkt() + "', " 
				+"Revurdering ='" + hObj.getRevurdering() + "'"
				+ " WHERE HId = '"
				+ hObj.gethId() + "'";
		System.out.println("Update query:" + query);

		try{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
		}
		catch(Exception ex){
			System.out.println("behPlanen kunne ikke opdateres: " + ex);
		}
		
		return rc;
	}
	
	public int sletBehPlan(int hId)
    {
       int rc = -1;
       PreparedStatement pstmt = null;
       String delete = "delete from Handling where hId = ?";
       System.out.println(delete);
         try
         {
            pstmt = con.prepareStatement(delete);
            pstmt.setInt(1,hId);
            rc = pstmt.executeUpdate();
            System.out.println("Behandlings planen er slettet fra databasen");
         }
         catch(SQLException sqlE)
         {
             System.out.println("SQL Error");
             System.out.println(sqlE.getMessage());   
         }
         catch(Exception e){
             e.getMessage();
             
         }
        return rc;
    }
	
	public Handling findBehPlan(int hId) {
		String wClause = " Handling.hId = '" + hId + "'";
		return singleWhere(wClause);
	}
	
	private Handling singleWhere(String wClause){
		ResultSet results;
		Handling h = new Behandlingsplan();
		
		String query = buildQuery(wClause);
		System.out.println(query);
		try{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			
			if(results.next()){
				h = buildBehPlan(results);
				stmt.close();
			} else {
				h = null;
			}
			
		}
		catch(Exception exe){
			
		}
		return h;
	}
	
	private String buildQuery(String wClause) {
		String query = "SELECT * FROM Handling";
		if (wClause.length() > 0) {
			query = query + " WHERE " + wClause;
		}
		return query;
	}
	
	private Handling buildBehPlan(ResultSet results) {
		Handling h = new Behandlingsplan();

		try {
			h.sethId(results.getInt("hId"));
			Indlaeggelse i = Dbindlaeggelse.findIndlaeggelse(results.getInt("iId"));
			h.setiId(i);
			h.setGennemfoertDato(results.getString("GennemfoertDato"));
			h.setUdloebsDato(results.getString("udloebsDato"));
			h.setPlanlagtDato(results.getString("PlanlagtDato"));
			h.setGennemfoertTidspunkt(results.getString("GennemfoertTidspunkt"));
			h.setPlanlagtTidspunkt(results.getString("PlanlagtTidspunkt"));
			h.setUdloebsTidspunkt(results.getString("UdloebsTidspunkt"));
			h.setRevurdering(results.getString("revurdering"));
			
		} 
		catch (Exception e) 
		{
			System.out.println("der skete en fejl");
		}

		 return h;

	}
	
	
	public ArrayList<Handling> alleBehandlingsPlanerPaaIndlaeggelse(Indlaeggelse i)
	{
		String wClause = " Handling.iid = '"+i.getiId()+"' and handling.handlingtype = 'behandlings plan'";
		return BehandlingsPlanerPaaIndlaeggelse(wClause);
	}
	
	
	private ArrayList<Handling> BehandlingsPlanerPaaIndlaeggelse(String wClause) 
	{
		ResultSet results;
		ArrayList<Handling> list = new ArrayList<Handling>();

		String query = buildQuery(wClause);

		try 
		{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) 
			{
				Handling bObj = new Behandlingsplan();
				bObj = buildBehPlan(results);
				list.add(bObj);
			}// end while
			stmt.close();
			

		}// slut try
		catch (Exception e) 
		{
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;
	}
	
	
	
}
